/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vigenere;

import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;

/**
 * @author triasfahrudin
 */
public class VigenereEnc extends MIDlet implements CommandListener {

    private Form formMain;
    private Form formResult;
    private Display display;
    private TextField plaintext,key;
    private Command enc;
    
            
    public VigenereEnc(){
        plaintext = new TextField("Plain text", "", 30, TextField.ANY);
        key = new TextField("key", "",30, TextField.ANY);
        enc = new Command("ENC",Command.OK,2);
        
    }
    
    public void startApp() {
        display = Display.getDisplay(this);
        formMain = new Form("Vigenere Enc");
        formMain.append(plaintext);
        formMain.append(key);
        formMain.addCommand(enc);
        formMain.setCommandListener(this);
        display.setCurrent(formMain);        
    }
    
    public void pauseApp() {
    }
    
    public void destroyApp(boolean unconditional) {
        notifyDestroyed();
    }
    
    public void showResult(){
        display = Display.getDisplay(this);
        int tableRowSize = 26;
        int tableColumnSize = 26;
        int vigenereTable[][] = new int[26][26];
        
        //generate vigenere table
        for (int rows = 0;rows < tableRowSize;rows ++){
            for(int columns = 0;columns < tableColumnSize;columns++){
                vigenereTable[rows][columns] = (rows + columns) % 26;
            }
        }
        
        String sPlainText = plaintext.getString().toUpperCase();
        String sKey = key.getString().toUpperCase();
        
        String cipherText = "";
        int keyIndex = 0;
        
        for(int ptextIndex = 0;ptextIndex < sPlainText.length();ptextIndex++){
            char pChar = sPlainText.charAt(ptextIndex);
            int asciiVal = (int) pChar;
            if(pChar == ' '){
                cipherText +=pChar;
                continue;
            }
            
            if (asciiVal < 65 || asciiVal > 90){
                cipherText +=pChar;
                continue;
            }
            int basicPlainTextValue = ((int) pChar - 65);
            char kChar = sKey.charAt(keyIndex);
            int basicKeyValue = ((int) kChar) - 65;
            int tableEntry = vigenereTable[basicPlainTextValue][basicKeyValue];
            char cChar = (char) (tableEntry + 65);
            cipherText +=cChar;
            keyIndex++;
            
            if(keyIndex == sKey.length()){
                keyIndex = 0;
            }
        }
        
        
        formResult = new Form("Result");
        formResult.append("Plain Text: " + sPlainText);
        formResult.append("chiper Text: " + cipherText);
        formResult.addCommand(new Command("Back",Command.BACK,1));
        formResult.setCommandListener(this);
        display.setCurrent(formResult);
        
        
    }
    
    public void commandAction(Command c,Displayable d){
        String label = c.getLabel();
        if(label.equals("ENC")){
            showResult();
        } else if(label.equals("Back")){
            display.setCurrent(formMain);
        }
        
    }
}
